//
//  PTOtherEmail.swift
//  PointTest28
//
//  Created by sc-chen on 2023/6/28.
//

import SwiftUI

struct PTOtherEmail: View {
    @State private var phone = ""
    @State private var msgContent = ""
    @State private var subject = ""
    
    @State private var isShowingHUD = false
    @State private var detailTxt = "不支持开发功能"
    
    var body: some View {
        ZStack {
            ScrollView {
                VStack {
                    HStack {
                        Text("收件人地址")
                            .padding()
                        Spacer()
                    }
                    TextField("请输入", text: $phone)
                        .overlay(
                            RoundedRectangle(cornerRadius: 10)
                                .stroke(Color.gray, lineWidth: 1)
                        )
                        .frame(height: 40)
                        .buttonStyle(.borderless)
                    
                    HStack {
                        Text("主题")
                            .padding()
                        Spacer()
                    }
                    TextField("请输入", text: $subject)
                        .overlay(
                            RoundedRectangle(cornerRadius: 10)
                                .stroke(Color.gray, lineWidth: 1)
                        )
                        .frame(height: 40)
                        .buttonStyle(.borderless)
                    
                    HStack {
                        Text("邮件内容")
                            .padding()
                        Spacer()
                    }
                    TextField("请输入", text: $msgContent)
                        .overlay(
                            RoundedRectangle(cornerRadius: 10)
                                .stroke(Color.gray, lineWidth: 1)
                        )
                        .frame(height: 40)
                        .buttonStyle(.borderless)
                    
//                    VStack {
//                        Button {
//                            showHud(text: "不支持开发功能")
//                        } label: {
//                            Text("邮件发送")
//                                .foregroundColor(.white)
//                                .padding()
//                                .frame(maxWidth: .infinity,maxHeight: 50)
//                                .background(
//                                    RoundedRectangle(cornerRadius: 5)
//                                        .fill(Color.blue)
//                                )
//                        }
//                        .buttonStyle(.borderless)
//                        .padding()
//                    }.padding(.top,40)
                    
                    VStack {
                        Button {
                            guard !phone.isEmpty else {
                                showHud(text: "请输入电话号码")
                                return
                            }
                            guard !subject.isEmpty else {
                                showHud(text: "请输入主题")
                                return
                            }
                            guard !msgContent.isEmpty else {
                                showHud(text: "请输入邮件内容")
                                return
                            }
                            
                            let subjectEncoded = subject.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
                            let bodyEncoded = msgContent.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
                                
                            let urlString = "mailto:?subject=\(subjectEncoded)&body=\(bodyEncoded)"
                                
                            if let url = URL(string: urlString) {
                                WKExtension.shared().openSystemURL(url)
                            }
                        } label: {
                            Text("系统邮件发送")
                                .foregroundColor(.white)
                                .padding()
                                .frame(maxWidth: .infinity,maxHeight: 50)
                                .background(
                                    RoundedRectangle(cornerRadius: 5)
                                        .fill(Color.blue)
                                )
                        }
                        .buttonStyle(.borderless)
                        .padding()
                    }.padding(.top,20)
                    Spacer()
                }
            }
            PTProgressHud(isPresented: $isShowingHUD, text: $detailTxt)
        }
    }
    
    private func showHud(text: String) {
        isShowingHUD = true
        detailTxt = text
        DispatchQueue.main.asyncAfter(deadline: .now()+2){
            isShowingHUD = false
        }
    }
    
    private func callEmailBySys(){
        
    }
}

struct PTOtherEmail_Previews: PreviewProvider {
    static var previews: some View {
        PTOtherEmail()
    }
}
